package com.dminvoicemanager.reports;

import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import com.dminvoicemanager.DM_InvoiceMenu;
import com.dminvoicemanager.DM_Reports_Generator;
import com.dminvoicemanager.R;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

public class DM_Sales_Summary_Report_Invoice extends Activity {
	private static final String TAG = null;
	private String DBNAME = "dminvoicemanager.db";
	private String TEMP_REP_INVOICESUMERY = "frepInvoiceSumery";
	private String INV_DETAILS = "FinvDet";
	private String INV_HEADER = "FinvHed";
	private String USER_SETTINGS = "usersettings";
	String item_result_1 = "0", item_result_2 = "0", item_result_3 = "0",
			item_result_4 = "0", item_result_5 = "0", item_result_6 = "0",
			item_result_7 = "0", item_result_8 = "0", item_result_9 = "0",
			item_result_10="0",item_result_13="0",PrintGrandTot;
	TextView txtHeadingReports, dmreportfromdate, dmreporttodate,
			dmdisplayrepname,  totalSum;
	String currentReportName, selectFromDate, selectToDate, currentUserTemp,
			currentUserPrefix, UserLocCode,selectFromDateNow,selectToDateNow;
	String CurrentDebtor = "0";
	DecimalFormat numberFormatAmt = new DecimalFormat("#,###,###.00");
	DecimalFormat numberFormat = new DecimalFormat("#.000000");
	int item_result_tot_2 = 0, item_result_tot_3 = 0,
			item_result_tot_4 = 0, item_result_tot_5 = 0, item_result_tot_6 = 0,
			item_result_tot_7 = 0, item_result_tot_8 = 0,item_result_tot_9 = 0,PrintGrandTot_tot = 0;
	double  item_result_tot_10 = 0.0,item_result_tot_13= 0.0;
	
	int loopPrint = 0;
	int toDayTag = 0;
	SimpleDateFormat originalFormat = new SimpleDateFormat("dd MM yyyy");
	SimpleDateFormat targetFormat = new SimpleDateFormat("yyyy-MM-dd");
	Date date, LoadingFromDate, LoadingToDate, LoadingOriDate;
	double totalValueSum = 0;
	public void BackToMainMenu(View view) {
		Intent myIntent = new Intent(view.getContext(), DM_Reports_Generator.class);
		myIntent.putExtra("TAGID", "RID/RNAME");
		myIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
				| Intent.FLAG_ACTIVITY_NEW_TASK);
		startActivityForResult(myIntent, 0);
		finish();
		return;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.dm_sales_summary_report);

		Bundle bundle = getIntent().getExtras();

		currentReportName = bundle.getString("REPORTNAME");
		selectFromDate = bundle.getString("FROMDATE");
		selectToDate = bundle.getString("TODATE");
		
		if(selectFromDate.equals(selectToDate)){
			toDayTag = 33;
			selectFromDateNow = selectFromDate.trim();
		}
		else{
			try {
				selectFromDateNow = selectFromDate.toString().trim();
				date = originalFormat.parse(selectFromDate.replaceAll("[-+/.^:,]",
						" "));
				Log.v(TAG, "Old Format From:" + originalFormat.format(date));
				Log.v(TAG, "New Format From:" + targetFormat.format(date));
				selectFromDate = targetFormat.format(date);
				LoadingFromDate = (Date) targetFormat.parse(selectFromDate);
				
				
				Log.v(TAG, "New Format From:" + selectFromDateNow);
			} catch (ParseException ex) {

			}
			
			try {
				selectToDateNow = selectToDate.toString().trim();
				date = originalFormat.parse(selectToDate.replaceAll("[-+/.^:,]",
						" "));
				Log.v(TAG, "Old Format To:" + originalFormat.format(date));
				Log.v(TAG, "New Format To:" + targetFormat.format(date));
				selectToDate = targetFormat.format(date);
				LoadingToDate = (Date) targetFormat.parse(selectToDate);
				
				
				Log.v(TAG, "New Format To:" + selectToDateNow);

			} catch (ParseException ex) {

			}
			
		}
		Log.v(TAG, "F DATE :" + selectFromDate);
		Log.v(TAG, "T DATE :" + selectToDate);
		txtHeadingReports = (TextView) findViewById(R.id.txtHeadingReports);
		txtHeadingReports.setText(currentReportName);
		dmreportfromdate = (TextView) findViewById(R.id.dmreportfromdate);
		dmreportfromdate.setText(selectFromDate);
		dmreporttodate = (TextView) findViewById(R.id.dmreporttodate);
		dmreporttodate.setText(selectToDate);
		dmdisplayrepname = (TextView) findViewById(R.id.dmdisplayrepname);
		  totalSum  = (TextView) findViewById(R.id.totalSum);
		
		
		currentuserTemp();
		// create the grid item mapping

		showCurrentInvoiceItemsLoading();
		// showDailySalesSummary();
	}

	private void currentuserTemp() {
		// TODO Auto-generated method stub
		SQLiteDatabase mydb;
		mydb = openOrCreateDatabase(DBNAME, Context.MODE_PRIVATE, null);
		Cursor selectCurrentuserrec = null;
		try {
			selectCurrentuserrec = mydb.query(USER_SETTINGS, null, null, null,
					null, null, null);
		} catch (Exception e) {
		}
		if (selectCurrentuserrec != null) {
			if (selectCurrentuserrec.getCount() > 0) {
				while (selectCurrentuserrec.moveToNext()) {
					currentUserTemp = selectCurrentuserrec.getString(1);
					currentUserPrefix = selectCurrentuserrec.getString(2);
					UserLocCode = selectCurrentuserrec.getString(5);
					dmdisplayrepname.setText(currentUserTemp);
				}
			}
		}
		selectCurrentuserrec.close();
		mydb.close();
	}

	private void showCurrentInvoiceItemsLoading() {
		// TODO Auto-generated method stub
		ListView lvtypedes = (ListView) findViewById(R.id.dminvoiceitemsdetlist);
		// create the grid item mapping
		String[] fromtrdes = new String[] { "item_result_1", "item_result_2",
				"item_result_3", "item_result_4", "item_result_5",
				"item_result_6", "item_result_7", "item_result_8",
				"item_result_9" ,"item_result_10","item_result_13" };

		int[] totrdes = new int[] { R.id.item_name, R.id.record_1,
				R.id.record_2, R.id.record_3, R.id.record_4, R.id.record_5,
				R.id.record_6, R.id.record_7, R.id.record_8 , R.id.record_9,R.id.record_12 };

		// prepare the list of all records
		List<HashMap<String, String>> fillMapstrdes = new ArrayList<HashMap<String, String>>();

		SQLiteDatabase mydb;
		mydb = openOrCreateDatabase(DBNAME, Context.MODE_PRIVATE, null);
		Cursor allrowitemsdes = null;
		String columsup[] = { "refInvoiceDetCode" };
		// String FromDate = "2013-10-21", ToDate = "2013-10-22";
		//Log.v(TAG, "TO DATE:" + selectToDate.trim());
		
		//Log.v(TAG, "Form DATE:" + selectFromDateNow.trim());
		//Log.v(TAG, "To DATE:" + selectToDateNow.trim());
		
		//selectFromDate = selectFromDate.trim();
		//selectFromDateNow = selectFromDateNow.trim(); //"11/02/2014";
		//selectToDateNow = selectToDateNow.trim(); //"12/02/2014";
		String query;
		try {
			
			if(toDayTag == 33){
				query = "SELECT FD.Itemcode,FD.itemDescription, FH.RefNo, FD.RefNo, sum(case when FD.TxnType = 22 then FD.CaseQty else 0 end),sum(case when FD.TxnType = 25 then FD.CaseQty else 0 end),sum(case when FD.TxnType = 27 then FD.CaseQty else 0 end),sum(case when FD.TxnType = 31 then FD.CaseQty else 0 end),sum(case when FD.TxnType = 22 then FD.PiceQty else 0 end),sum(case when FD.TxnType = 25 then FD.PiceQty else 0 end),sum(case when FD.TxnType = 27 then FD.PiceQty else 0 end),sum(case when FD.TxnType = 31 then FD.PiceQty else 0 end),sum(case when FD.TxnType = 22 then Amt else 0 end),fS.Tonnage,sum(case when FD.TxnType = 22 then Qty else 0 end),(fS.Tonnage * sum(case when FD.TxnType = 22 then Qty else 0 end)) from FinvDet FD, FinvHed FH, FSKU as FS,fItems as FI Where FD.TxnDate = '"
						+ selectFromDateNow.trim()
						+ "' AND FH.RefNo = FD.RefNo AND FI.SKUCode = FS.SKUCode AND FI.ItemCode = fd.Itemcode AND FD.invdetSync <> 3  group by FD.Itemcode, FH.RefNo, FD.RefNo,FD.Amt,fS.Tonnage order by FH.RefNo";

			}
			else{
				query = "SELECT FD.Itemcode,FD.itemDescription, FH.RefNo, FD.RefNo, sum(case when FD.TxnType = 22 then FD.CaseQty else 0 end),sum(case when FD.TxnType = 25 then FD.CaseQty else 0 end),sum(case when FD.TxnType = 27 then FD.CaseQty else 0 end),sum(case when FD.TxnType = 31 then FD.CaseQty else 0 end),sum(case when FD.TxnType = 22 then FD.PiceQty else 0 end),sum(case when FD.TxnType = 25 then FD.PiceQty else 0 end),sum(case when FD.TxnType = 27 then FD.PiceQty else 0 end),sum(case when FD.TxnType = 31 then FD.PiceQty else 0 end),sum(case when FD.TxnType = 22 then Amt else 0 end),fS.Tonnage,sum(case when FD.TxnType = 22 then Qty else 0 end),(fS.Tonnage * sum(case when FD.TxnType = 22 then Qty else 0 end)) from FinvDet FD, FinvHed FH, FSKU as FS,fItems as FI Where fd.txndate >= '"
						+ selectFromDateNow
						+ "' AND fd.txndate <= '"
						+ selectToDateNow.trim()
						+ "' AND FH.RefNo = FD.RefNo AND FI.SKUCode = FS.SKUCode AND FI.ItemCode = fd.Itemcode AND FD.invdetSync <> 3  group by FD.Itemcode, FH.RefNo, FD.RefNo,FD.Amt,fS.Tonnage order by FH.RefNo";

			}
			
			// ***Select sum queries need seperately 8 to for the combinations
			allrowitemsdes = mydb.rawQuery(query, null);
			Log.v(TAG, "Count Invoices:" + allrowitemsdes.getCount());
			if (allrowitemsdes != null) {
				if (allrowitemsdes.getCount() > 0) {
					while (allrowitemsdes.moveToNext()) {
						// itemDescription
						loopPrint = loopPrint + 1;
						item_result_1 = allrowitemsdes.getString(1);
						String CurrentDebtorRecord = allrowitemsdes
								.getString(2);
						String rawAmount = allrowitemsdes
								.getString(12);
						double rawAmountVal = Double.parseDouble(rawAmount.replaceAll("[,]", ""));
						totalValueSum = totalValueSum + rawAmountVal;
						
						PrintGrandTot = Double
								.toString(totalValueSum);
						
						
						
						Log.v(TAG, "totalValueSum 99:" + totalValueSum);
						if (CurrentDebtor.equals(CurrentDebtorRecord)) {
							CurrentDebtor = CurrentDebtorRecord.trim();
							Log.v(TAG, "Debtor 1:" + CurrentDebtor);
							String Debtor = allrowitemsdes.getString(3);
							// Log.v(TAG, "Debtor:"+ Debtor);
							// Case Qty sales 22 SA
							item_result_2 = allrowitemsdes.getString(4);
							int item_result_2Val = Integer.parseInt(item_result_2.replaceAll("[,]", ""));
							item_result_tot_2  =  item_result_tot_2 + item_result_2Val;
							
							// Case Qty ureturn 25 UR
							item_result_3 = allrowitemsdes.getString(5);
							int item_result_3Val = Integer.parseInt(item_result_3.replaceAll("[,]", ""));
							item_result_tot_3  =  item_result_tot_3 + item_result_3Val;
							
							// Case Qty fissue 27 FI
							item_result_4 = allrowitemsdes.getString(6);
							int item_result_4Val = Integer.parseInt(item_result_4.replaceAll("[,]", ""));
							item_result_tot_4  =  item_result_tot_4 + item_result_4Val;
							
							// Case Qty mreturn 31 MR
							item_result_5 = allrowitemsdes.getString(7);
							int item_result_5Val = Integer.parseInt(item_result_5.replaceAll("[,]", ""));
							item_result_tot_5  =  item_result_tot_5 + item_result_5Val;
							
							// Pices Qty sales 22 SA
							item_result_6 = allrowitemsdes.getString(8);
							int item_result_6Val = Integer.parseInt(item_result_6.replaceAll("[,]", ""));
							item_result_tot_6  =  item_result_tot_6 + item_result_6Val;
							
							// Pices Qty ureturn 25 UR
							item_result_7 = allrowitemsdes.getString(9);
							int item_result_7Val = Integer.parseInt(item_result_7.replaceAll("[,]", ""));
							item_result_tot_7  =  item_result_tot_7 + item_result_7Val;
							
							// Pices Qty fissue 27 FI
							item_result_8 = allrowitemsdes.getString(10);
							int item_result_8Val = Integer.parseInt(item_result_8.replaceAll("[,]", ""));
							item_result_tot_8  =  item_result_tot_8 + item_result_8Val;
							
							// Pices Qty mreturn 31 MR
							item_result_9 = allrowitemsdes.getString(11);
							int item_result_9Val = Integer.parseInt(item_result_9.replaceAll("[,]", ""));
							item_result_tot_9  =  item_result_tot_9 + item_result_9Val;
							
							// Total Amount
							item_result_10 = allrowitemsdes.getString(12);
							double item_result_10Val = Double.parseDouble(item_result_10.replaceAll("[,]", ""));
							String Stritem_result_10Val = numberFormatAmt.format(item_result_10Val);
							item_result_tot_10  =  item_result_tot_10 + item_result_10Val;
							
							//tonage
							item_result_13 = allrowitemsdes.getString(15);
							double item_result_13Val = Double.parseDouble(item_result_13.replaceAll("[,]", ""));
							String Stritem_result_13Val = numberFormat.format(item_result_13Val);
							item_result_tot_13  =  item_result_tot_13 + item_result_13Val;
							
							Log.v(TAG, "item_result_10 99:" + allrowitemsdes.getString(12));
							

							HashMap<String, String> maptrdes = new HashMap<String, String>();
							maptrdes.put("item_result_1", item_result_1);
							// print Case Qty sales 22 SA
							maptrdes.put("item_result_2", item_result_2);
							// print Pices Qty sales 22 SA
							maptrdes.put("item_result_3", item_result_6);
							// print Case Qty ureturn 25 UR
							maptrdes.put("item_result_4", item_result_3);
							// print Pices Qty ureturn 25 UR
							maptrdes.put("item_result_5", item_result_7);
							// print Case Qty mreturn 31 MR
							maptrdes.put("item_result_8", item_result_4);
							// print Pices Qty mreturn 31 MR
							maptrdes.put("item_result_9", item_result_8);
							// print Case Qty fissue 27 FI
							maptrdes.put("item_result_6", item_result_5);
							// print Pices Qty fissue 27 FI
							maptrdes.put("item_result_7", item_result_9);
							// print Total Value
							maptrdes.put("item_result_10", Stritem_result_10Val);
							// Tonage
							maptrdes.put("item_result_13", Stritem_result_13Val);
							
							Log.v(TAG, "item_result_10 98:" + item_result_10);

							// fill in the grid_item layout
							fillMapstrdes.add(maptrdes);
							SimpleAdapter adaptertrdes = new SimpleAdapter(
									this, fillMapstrdes,
									R.layout.dm_sales_summary_report_raw,
									fromtrdes, totrdes);
							lvtypedes.setAdapter(adaptertrdes);
						} else {
							// ***Add Seperator raw View
							if (loopPrint > 1) {

								HashMap<String, String> maptrdesBlank = new HashMap<String, String>();
								maptrdesBlank.put("item_result_1", "");
								// print Case Qty sales 22 SA
								maptrdesBlank.put("item_result_2", "");
								// print Pices Qty sales 22 SA
								maptrdesBlank.put("item_result_3", "");
								// print Case Qty ureturn 25 UR
								maptrdesBlank.put("item_result_4", "");
								// print Pices Qty ureturn 25 UR
								maptrdesBlank.put("item_result_5", "");
								// print Case Qty mreturn 31 MR
								maptrdesBlank.put("item_result_8", "");
								// print Pices Qty mreturn 31 MR
								maptrdesBlank.put("item_result_9", "");
								// print Case Qty fissue 27 FI
								maptrdesBlank.put("item_result_6", "");
								// print Pices Qty fissue 27 FI
								maptrdesBlank.put("item_result_7", "");
								// print Total 
								maptrdesBlank.put("item_result_10", "");
								// print tonage
								maptrdesBlank.put("item_result_13", "");
								Log.v(TAG, "item_result_10 97:" + item_result_10);

								// fill in the grid_item layout
								fillMapstrdes.add(maptrdesBlank);
								SimpleAdapter adaptertrdesBlank = new SimpleAdapter(
										this, fillMapstrdes,
										R.layout.dm_sales_summary_report_raw,
										fromtrdes, totrdes);
								lvtypedes.setAdapter(adaptertrdesBlank);
							}
							// *****Add Retailer Name In List***********
							CurrentDebtor = CurrentDebtorRecord.trim();
							Log.v(TAG, "Debtor 2:" + CurrentDebtor);
							String CurrentDebtorName = allrowitemsdes
									.getString(3);
							HashMap<String, String> maptrdes = new HashMap<String, String>();
							maptrdes.put("item_result_1", CurrentDebtorName);
							// print Case Qty sales 22 SA
							maptrdes.put("item_result_2", "");
							// print Pices Qty sales 22 SA
							maptrdes.put("item_result_3", "");
							// print Case Qty ureturn 25 UR
							maptrdes.put("item_result_4", "");
							// print Pices Qty ureturn 25 UR
							maptrdes.put("item_result_5", "");
							// print Case Qty mreturn 31 MR
							maptrdes.put("item_result_8", "");
							// print Pices Qty mreturn 31 MR
							maptrdes.put("item_result_9", "");
							// print Case Qty fissue 27 FI
							maptrdes.put("item_result_6", "");
							// print Pices Qty fissue 27 FI
							maptrdes.put("item_result_7", "");
							// print Total 
							maptrdes.put("item_result_10", "");
							// print tonage 
							maptrdes.put("item_result_13", "");
							
							Log.v(TAG, "item_result_10 96:" + item_result_10);

							// fill in the grid_item layout
							fillMapstrdes.add(maptrdes);
							SimpleAdapter adaptertrdes = new SimpleAdapter(
									this, fillMapstrdes,
									R.layout.dm_sales_summary_report_raw,
									fromtrdes, totrdes);
							lvtypedes.setAdapter(adaptertrdes);

							// *****Print Current Record***********
							item_result_1 = allrowitemsdes.getString(1);
							// Case Qty sales 22 SA
							item_result_2 = allrowitemsdes.getString(4);
							int item_result_2Val = Integer.parseInt(item_result_2.replaceAll("[,]", ""));
							item_result_tot_2  =  item_result_tot_2 + item_result_2Val;
							
							// Case Qty ureturn 25 UR
							item_result_3 = allrowitemsdes.getString(5);
							int item_result_3Val = Integer.parseInt(item_result_3.replaceAll("[,]", ""));
							item_result_tot_3  =  item_result_tot_3 + item_result_3Val;
							
							// Case Qty fissue 27 FI
							item_result_4 = allrowitemsdes.getString(6);
							int item_result_4Val = Integer.parseInt(item_result_4.replaceAll("[,]", ""));
							item_result_tot_4  =  item_result_tot_4 + item_result_4Val;
							
							// Case Qty mreturn 31 MR
							item_result_5 = allrowitemsdes.getString(7);
							int item_result_5Val = Integer.parseInt(item_result_5.replaceAll("[,]", ""));
							item_result_tot_5  =  item_result_tot_5 + item_result_5Val;
							
							// Pices Qty sales 22 SA
							item_result_6 = allrowitemsdes.getString(8);
							int item_result_6Val = Integer.parseInt(item_result_6.replaceAll("[,]", ""));
							item_result_tot_6  =  item_result_tot_6 + item_result_6Val;
							
							// Pices Qty ureturn 25 UR
							item_result_7 = allrowitemsdes.getString(9);
							int item_result_7Val = Integer.parseInt(item_result_7.replaceAll("[,]", ""));
							item_result_tot_7  =  item_result_tot_7 + item_result_7Val;
							
							// Pices Qty fissue 27 FI
							item_result_8 = allrowitemsdes.getString(10);
							int item_result_8Val = Integer.parseInt(item_result_8.replaceAll("[,]", ""));
							item_result_tot_8  =  item_result_tot_8 + item_result_8Val;
							
							// Pices Qty mreturn 31 MR
							item_result_9 = allrowitemsdes.getString(11);
							int item_result_9Val = Integer.parseInt(item_result_9.replaceAll("[,]", ""));
							item_result_tot_9  =  item_result_tot_9 + item_result_9Val;
							
							// Total Amount
							item_result_10 = allrowitemsdes.getString(12);
							double item_result_10Val = Double.parseDouble(item_result_10.replaceAll("[,]", ""));
							String Stritem_result_10Val = numberFormatAmt.format(item_result_10Val);
							item_result_tot_10  =  item_result_tot_10 + item_result_10Val;
							
							//tonage
							item_result_13 = allrowitemsdes.getString(15);
							double item_result_13Val = Double.parseDouble(item_result_13.replaceAll("[,]", ""));
							String Stritem_result_13Val = numberFormat.format(item_result_13Val);
							item_result_tot_13  =  item_result_tot_13 + item_result_13Val;
							
							Log.v(TAG, "item_result_10 96:" + item_result_10);

							HashMap<String, String> maptrdescu = new HashMap<String, String>();
							maptrdescu.put("item_result_1", item_result_1);
							// print Case Qty sales 22 SA
							maptrdescu.put("item_result_2", item_result_2);
							// print Pices Qty sales 22 SA
							maptrdescu.put("item_result_3", item_result_6);
							// print Case Qty ureturn 25 UR
							maptrdescu.put("item_result_4", item_result_3);
							// print Pices Qty ureturn 25 UR
							maptrdescu.put("item_result_5", item_result_7);
							// print Case Qty mreturn 31 MR
							maptrdescu.put("item_result_8", item_result_4);
							// print Pices Qty mreturn 31 MR
							maptrdescu.put("item_result_9", item_result_8);
							// print Case Qty fissue 27 FI
							maptrdescu.put("item_result_6", item_result_5);
							// print Pices Qty fissue 27 FI
							maptrdescu.put("item_result_7", item_result_9);
							// print Amt
							maptrdescu.put("item_result_10", Stritem_result_10Val);
							// print Tonage
							maptrdescu.put("item_result_13", Stritem_result_13Val);
							
							Log.v(TAG, "item_result_10 95:" + item_result_10);
							

							// fill in the grid_item layout
							fillMapstrdes.add(maptrdescu);
							SimpleAdapter adaptertrdescu = new SimpleAdapter(
									this, fillMapstrdes,
									R.layout.dm_sales_summary_report_raw,
									fromtrdes, totrdes);
							lvtypedes.setAdapter(adaptertrdescu);
						}
					}
					
					
					HashMap<String, String> maptrdes = new HashMap<String, String>();
					maptrdes.put("item_result_1", "Grand Total");
					// print Case Qty sales 22 SA
					String item_result_totval_2 = Integer.toString(item_result_tot_2);
					maptrdes.put("item_result_2", item_result_totval_2);
					
					// print Pices Qty sales 22 SA
					String item_result_totval_6 = Integer.toString(item_result_tot_6);
					maptrdes.put("item_result_3", item_result_totval_6);
					
					// print Case Qty ureturn 25 UR
					String item_result_totval_3 = Integer.toString(item_result_tot_3);
					maptrdes.put("item_result_4", item_result_totval_3);
					
					// print Pices Qty ureturn 25 UR
					String item_result_totval_7 = Integer.toString(item_result_tot_7);
					maptrdes.put("item_result_5", item_result_totval_7);
					
					// print Case Qty mreturn 31 MR
					String item_result_totval_4 = Integer.toString(item_result_tot_4);
					maptrdes.put("item_result_8", item_result_totval_4);
					
					// print Pices Qty mreturn 31 MR
					String item_result_totval_8 = Integer.toString(item_result_tot_8);
					maptrdes.put("item_result_9", item_result_totval_8);
					
					// print Case Qty fissue 27 FI
					String item_result_totval_5 = Integer.toString(item_result_tot_5);
					maptrdes.put("item_result_6", item_result_totval_5);
					
					// print Pices Qty fissue 27 FI
					String item_result_totval_9 = Integer.toString(item_result_tot_9);
					maptrdes.put("item_result_7", item_result_totval_9);
					
					// Amt
					
					String Stritem_result_totval_10 = numberFormatAmt.format(item_result_tot_10);
					maptrdes.put("item_result_10", Stritem_result_totval_10);
					
					// Tonage
					
					String Stritem_result_tot_13 = numberFormat.format(item_result_tot_13);
					/*String item_result_totval_13 = Double.toString(item_result_tot_13);*/
					maptrdes.put("item_result_13", Stritem_result_tot_13);
					
					
					// fill in the grid_item layout
					fillMapstrdes.add(maptrdes);
					SimpleAdapter adaptertrdes = new SimpleAdapter(this,
							fillMapstrdes,
							R.layout.dm_sales_summary_report_raw,
							fromtrdes, totrdes);
					lvtypedes.setAdapter(adaptertrdes);
					totalSum.setText(PrintGrandTot);
				}
			}
			allrowitemsdes.close();
			mydb.close();
		} catch (Exception e) {
		}

	}

	@Override
	public void onBackPressed() {
		// Do Here what ever you want do on back press;
	}
}
